package com.wing.productionPlan.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.productionPlan.dao.ProductionProcessMaterialDao;
import com.wing.productionPlan.model.entity.ProductionProcessMaterial;
import com.wing.productionPlan.model.request.ProductionProcessMaterialForm;
import com.wing.productionPlan.model.response.ProductionProcessMaterialVO;
import com.wing.productionPlan.model.response.RecordMaterialSkuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 生产工序物料表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductionProcessMaterialServiceImpl extends ServiceImpl<ProductionProcessMaterialDao, ProductionProcessMaterial> implements ProductionProcessMaterialService {

    @Override
    public int add(ProductionProcessMaterialForm productionProcessMaterialForm) {

        //校验

        ProductionProcessMaterial productionProcessMaterial =new ProductionProcessMaterial();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductionProcessMaterial.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productionProcessMaterialForm, productionProcessMaterial, copyOptions);

        return baseMapper.insert(productionProcessMaterial);
    }

    @Override
    public List<ProductionProcessMaterialVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductionProcessMaterialVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductionProcessMaterialVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductionProcessMaterialVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<ProductionProcessMaterialVO> getSimpleWorkOrderMaterial(String skuCode, Long simpleWorkOrderId) {
        return baseMapper.getSimpleWorkOrderMaterial(skuCode,simpleWorkOrderId);
    }

    /**
     * 查询工序设备物料信息
     * @param productionPlanId 生产计划id
     * @param productionPlanRecordId 生产计划记录id
     * @param processId 工序id
     * @return
     */
    @Override
    public List<RecordMaterialSkuVo> getMaterialInfoBy(Long productionPlanId, Long productionPlanRecordId, Long processId) {
        QueryWrapper<ProductionProcessMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan_id", productionPlanId);

        queryWrapper.eq("plan_record_id", productionPlanRecordId);
        queryWrapper.eq("process_id", processId);
        List<ProductionProcessMaterial> list = this.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            List<RecordMaterialSkuVo> skuVos = new ArrayList<>();
            for (ProductionProcessMaterial productionProcessMaterial : list) {
                RecordMaterialSkuVo recordMaterialSkuVo = new RecordMaterialSkuVo();
                BeanUtil.copyProperties(productionProcessMaterial, recordMaterialSkuVo);
                skuVos.add(recordMaterialSkuVo);
            }
            return skuVos;
        }
        return null;
    }
}
